//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: IMM.c 757 2007-05-10 08:19:31Z roger $
//

/* FILE : IMM.c */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <string.h>
#include <stdio.h>
#include <iconv.h>
#include <standard_im.h>
#include <unihan_im.h>

#include "stdif.h"
#include "IMM.h"
#include "utils.h"

UnihanIM *UIM;

void
IMM_show_preedit_string	(const IME_InputContext *ic)
{
#if DEBUG
	fprintf(stderr, "STD IF : IMM_show_preedit_string()\n");
#endif
	StdifDataPerSession *ds = (StdifDataPerSession *)UIM->sessionUserData;
        IME_String *preedit = ds->preedit;
#if DEBUG
	fprintf(stderr, "STD IF : show preedit %s\n", preedit->string);
#endif

	UIM->m->preedit_draw(UIM, encoding_to_utf8(UIM, preedit->string), NULL);
        //uim->m->preedit_caret_pos(UIM, utf8_charlen(peString));
}

void
IMM_hide_preedit_string	(const IME_InputContext *ic)
{
#if DEBUG
	fprintf(stderr, "STD IF : IMM_hide_preedit_string()\n");
#endif
	UIM->m->preedit_erase(UIM);
	return;
}

void
IMM_update_preedit_string (const IME_InputContext *ic,
			   const IME_String *string)
{
#if DEBUG
	fprintf(stderr, "STD IF : IMM_update_preedit_string()\n");
#endif
	StdifDataPerSession *ds = (StdifDataPerSession *)UIM->sessionUserData;

	ds->preedit = (IME_String *)string;
}

void
IMM_update_preedit_caret (const IME_InputContext *ic,
			  int   caret)
{
	return;
}

void
IMM_show_aux_string (const IME_InputContext *ic)
{
	return;
}

void
IMM_hide_aux_string (const IME_InputContext *ic)
{
	return;
}

void
IMM_update_aux_string (const IME_InputContext *ic,
		       const IME_String *string)
{
	return;
}

void IMM_show_candidates_list (const IME_InputContext *ic)
{
#if DEBUG
	fprintf(stderr, "STD IF : IMM_show_candidates_list()\n");
#endif
	StdifDataPerSession *ds = (StdifDataPerSession *)UIM->sessionUserData;
	UnihanLookupDraw ludraw;
	int i;
	IME_CandidatesList *candidates_list = ds->candidates_list;
	IME_Candidate *candidates = candidates_list->candidates;

	if (ds->lookupShown == 0) {
		UnihanLookupStart lustart;
		lustart.choicePerWindow = 9;
		lustart.numColumns = 9;
		lustart.numRows = 1;
		lustart.direction = UNIHAN_LOOKUP_DIRECTION_RIGHT;
		UIM->m->lookup_start(UIM, &lustart);
		ds->lookupShown = 1;
	}

	ludraw.numChoices = candidates_list->nr_candidates;
	ludraw.selected = ds->luCurrCandOffset;
        ludraw.currentChoice = ds->luCurrCandOffset; 
        ludraw.totalChoices = candidates_list->nr_candidates;
	ludraw.title = "";
	UIM->m->mem_alloc(UIM, sizeof(char *) * ludraw.numChoices, (void**)&ludraw.label);
	UIM->m->mem_alloc(UIM, sizeof(char *) * ludraw.numChoices, (void**)&ludraw.value);

	for (i=0; i<ludraw.numChoices; ++i) {
		ludraw.label[i] = strdup(candidates[i].label);
		ludraw.value[i] = encoding_to_utf8(UIM, candidates[i].content.string);
	}

	UIM->m->lookup_draw(UIM, &ludraw);
}

void
IMM_hide_candidates_list (const IME_InputContext *ic)
{
#if DEBUG
	fprintf(stderr, "STD IF : IMM_hide_candidates_list()\n");
#endif
	UIM->m->lookup_done(UIM);
}

void
IMM_update_candidates_list (const IME_InputContext *ic,
			    const IME_CandidatesList *candidates)
{
#if DEBUG
	fprintf(stderr, "STD IF : IMM_update_candidates_list()\n");
#endif
	StdifDataPerSession *ds = (StdifDataPerSession *)UIM->sessionUserData;

	ds->candidates_list = (IME_CandidatesList *)candidates;
}

void
IMM_set_focused_candidate (const IME_InputContext *ic,
			  int   index)
{
	StdifDataPerSession *ds = (StdifDataPerSession *)UIM->sessionUserData;

	ds->luCurrCandOffset = index;
}

void
IMM_commit_string (const IME_InputContext *ic,
		   const char *string)
{
        UnihanTCSCMode tcsc;

        UIM->m->tcsc_mode_get(UIM, &tcsc);
        if (tcsc == UNIHAN_TCSC_NO_CONV) {
                UIM->m->commit_string(UIM, (char *)string);
        } else {
                char *cnvd = NULL;
                UIM->m->tcsc_do_conv(UIM, tcsc, (char *)string, &cnvd);
                UIM->m->commit_string(UIM, cnvd);
        }
}

void
IMM_register_properties (//const IME_InputContext *ic,
			const char *uuid,
			const IME_PropertiesList *properties)
{
	//UIM->m->
	return;
}

void
IMM_update_property (const IME_InputContext *ic,
		     const IME_Property *property)
{
	return;
}

void
IMM_read_config	(const char *key,
		 char  *buf,
		 size_t buf_size)
{
	return;
}

void
IMM_write_config(const char *key,
		const char *value)
{
	return;
}

void
IMM_load_user_data(const char *key,
		void **buf,
		size_t *buf_size)
{
	return;
}

void
IMM_free_user_data(void *buf)
{
	return;
}

void
IMM_save_user_data(const char *key,
		const void *buf,
		size_t buf_size)
{
	return;
}

void
IMM_start_helper(const IME_InputContext *ic,
		const char *helper_uuid)
{
	return;
}

void
IMM_stop_helper(const IME_InputContext *ic,
		const char *helper_uuid)
{
	return;
}

void
IMM_send_helper_event(const IME_InputContext *ic,
		const char *helper_uuid,
		const void *buf,
		size_t buf_size)
{
#if DEBUG
	fprintf(stderr, "STD IF : IMM send helper event\n");
#endif
	UIM->m->send_buffer(UIM, (char *)buf, buf_size);
	return;
}

void
IMM_beep(const IME_InputContext *ic, IME_Beep_Type type)
{
	return;
}

void
IMM_set_functions (IMM_Functions **imm)
{
#if DEBUG
	fprintf(stderr, "STD IF : IMM set functions\n");
#endif
	if (!imm || !(*imm))
		return;
	(*imm)->show_preedit_string = IMM_show_preedit_string;
	(*imm)->hide_preedit_string = IMM_hide_preedit_string;
	(*imm)->update_preedit_string = IMM_update_preedit_string;
	(*imm)->update_preedit_caret = IMM_update_preedit_caret;
	(*imm)->show_aux_string = IMM_show_aux_string;
	(*imm)->hide_aux_string = IMM_show_aux_string;
	(*imm)->update_preedit_string = IMM_update_preedit_string;
	(*imm)->show_candidates_list = IMM_show_candidates_list;
	(*imm)->hide_candidates_list = IMM_hide_candidates_list;
	(*imm)->update_candidates_list = IMM_update_candidates_list;
	(*imm)->set_focused_candidate = IMM_set_focused_candidate;
	(*imm)->commit_string = IMM_commit_string;
	(*imm)->register_properties = IMM_register_properties;
	(*imm)->update_property = IMM_update_property;
	(*imm)->read_config = IMM_read_config;
	(*imm)->write_config = IMM_write_config;
	(*imm)->load_user_data = IMM_load_user_data;
	(*imm)->free_user_data = IMM_free_user_data;
	(*imm)->save_user_data = IMM_save_user_data;
	(*imm)->start_helper = IMM_start_helper;
	(*imm)->stop_helper = IMM_stop_helper;
	(*imm)->send_helper_event = IMM_send_helper_event;
	(*imm)->beep = IMM_beep;
	return;
}

// vi:ts=4:nowrap:cin:expandtab
